import { defineUserConfig } from 'vuepress'
import * as config from 'config'
import type { DefaultThemeOptions } from 'vuepress'
const { tsCompilerOptions } = require('tsconfig.json')
const { viteCompilerOptions } = require('vite.config')
const { path } = require('@vuepress/utils')
const root_path =  config.default.host + config.default.root_path;

module.exports = defineUserConfig<DefaultThemeOptions>({
  bundler: '@vuepress/vite',
  bundlerConfig: {
    ...viteCompilerOptions
  },
  dest: 'dist',
  public: 'public',
  temp: "./node_modules/.temp",
  cache: "./node_modules/.cache",
  base: config.default.root_path,
  evergreen: !config.default.isProduction,
  extends: '@vuepress/theme-default',
  layouts: {
    Layout: path.resolve(__dirname, 'layouts/Layout.vue'),
    404: path.resolve(__dirname, '/layouts/404.vue'),
  },
  head: [
    ['meta', { name: 'charset',                                 content: 'utf-8'}],
    ['meta', { name: 'viewport',                                content: 'width=device-width,initial-scale=1,maximum-scale=1,user-scalable=0,viewport-fit=cover' }],
    ['meta', { name: 'renderer',                                content: config.default.defaultRenderer }],
    ['meta', { name: 'force-rendering',                         content: config.default.defaultRenderer}],
    ['meta', { name: 'applicable-device',                       content: 'pc,mobile'}],
    ['meta', { name: 'msapplication-titleColor',                content: config.default.themeColor}],
    ['meta', { name: 'theme-color',                             content: config.default.themeColor}],
    ['meta', { name: 'msappdivcation-navbutton-color',          content: config.default.themeColor}],
    ['meta', { name: 'apple-mobile-web-app-status-bar-style',   content: 'lack-translucent'}],
    ['meta', { name: 'apple-mobile-web-app-capable',            content: 'yes'}],
    ['meta', { name: 'apple-mobile-web-app-title',              content: config.default.site_name}],
    ['meta', { name: 'format-detection',                        content: 'telephone=no'}],
    ['meta', { name: 'google',                                  content: 'notranslate'}],
    ['meta', { name: 'twitter:widgets:csp',                     content: 'on'}],
    ['meta', { name: 'twitter:card',                            content: 'summary'}],
    ['meta', { name: 'twitter:url',                             content: config.default.host}],
    ['meta', { name: 'twitter:image',                           content: config.default.cover}],
    ['meta', { property: 'og:url',                              content: config.default.host}],
    ['meta', { property: 'og:image',                            content: config.default.cover}],
    ['meta', { property: 'og:site_name',                        content: config.default.site_name}],
    ['meta', { property: 'type',                                content: config.default.type}],
    ['meta', { property: 'article:author',                      content: 'ZengJia'}],
    ['meta', { property: 'article:published_time',              content: config.default.published_time}],
    ['meta', { property: 'image',                               content: config.default.cover}],
    ['meta', { property: 'site_name',                           content: config.default.site_name}],
    ['meta', { property: 'url',                                 content: config.default.host}],
    ['meta', { itemprop: 'name',                                content: config.default.site_name}],
    ['meta', { itemprop: 'image',                               content: config.default.cover}],
    ...config.default.dns_prefetch.map(val => ['link', {rel: 'dns-prefetch', href: val}]),
    ...config.default.preconnect.map(val => ['link', {rel: 'preconnect', href: val}]),
    ['link', { rel: 'manifest',                                 href: 'manifest.webmanifest' }],
    ['link', { rel: 'apple-touch-icon',                         href: config.default.apple_touch_icon }],
    ['link', { rel: 'shortcut icon',                            href: config.default.favicon}],
    ['link', { rel: 'mask-icon',                                href: root_path + config.default.mask_icon,           color: config.default.themeColor }],
    ['link', { rel: 'alternate',                                href: config.default.host + config.default.path_en,   hreflang: 'en-US'}],
    ['link', { rel: 'alternate',                                href: root_path,                                      hreflang: 'zh-Hans-CN'}],
    ['link', { rel: 'alternate',                                href: config.default.host + config.default.path_ja,   hreflang: 'ja-JP'}],
    ['link', { rel: 'alternate',                                href: root_path,                                      hreflang: 'x-default'}],
  ],
  locales: {

  },
  themeConfig: {
    logo: config.default.logo,
    repo: config.default.repo,
    docsRepo: config.default.repo,
    docsBranch: 'master',
    docsDir: 'docs/docs',
    editLinkPattern: ':repo/edit/:branch/:path',
    themePlugins: {
      git: config.default.isProduction,
    },
    locales: {
    }
  },
  plugins: [
    [
      'vuepress-plugin-typescript',
      {
        tsLoaderOptions: {
          ...tsCompilerOptions
        },
      },
    ],
    [
      '@vuepress/google-analytics',
      {
        id: config.default.analytics_id
      },
    ],
    [
      'vuepress-plugin-zooming',
      {
        selector: '.zooming',
        delay: 1000,
        options: {
          bgColor: 'black',
          zIndex: 10000,
        },
      },
    ],
    [
      '@vuepress/pwa',
      {
        skipWaiting: false,
      },
    ],
    [
      '@vuepress/plugin-pwa-popup',
      {
        locales: {
          '/zh/': {
            message: 'New content is available.',
            buttonText: 'Refresh',
          },
          '/': {
            message: '发现新内容可用',
            buttonText: '刷新',
          },
          '/ja/': {
            message: '利用可能な新しいコンテンツが見つかりました',
            buttonText: 'リフレッシュ',
          },
        },
      },
    ],
    [
      'redirect',
      {
        locales: true,
        redirectors: [
          {
            // base: '/plugins/', // 将 `/my-plugins/` 自动重定向到某个子页面
            storage: true, // 保存最后一次访问的结果到 `localStorage`，供下次重定向使用
            alternative: [ // 备选列表
            ],
          },
        ],
      },
    ],
    [
      '@vuepress/plugin-search',
      {
        maxSuggestions: 5,
        // hotKeys: ['s', '/'],
        locales: {
          '/en/': {
            placeholder: 'Search',
          },
          '/': {
            placeholder: '搜索',
          },
          '/ja/':{
            placeholder: '検索する',
          }
        },
      },
    ],
    // [
    //   '@vuepress/docsearch',
    //   {
    //     apiKey: '<API_KEY>',
    //     indexName: '<INDEX_NAME>',
    //     locales: {
    //       '/': {
    //         placeholder: 'Search',
    //       },
    //       '/zh/': {
    //         placeholder: '搜索',
    //       },
    //       '/ja/':{
    //         placeholder: '検索する',
    //       }
    //     },
    //   },
    // ],
    [
      '@vuepress/container',
      {
        type: 'tip',
        locales: {
          '/en/': {
            defaultInfo: 'Tips',
          },
          '/': {
            defaultInfo: '提示',
          },
          '/jp/':{
            defaultInfo: 'ヒント',
          }
        },
      },
    ],
    [
      '@vuepress/register-components',
      {
        componentsDir: path.resolve(__dirname, './docs/.vuepress/components'),
      },
    ],
    [
      '@vuepress/active-header-links',
      {
        sidebarLinkSelector: '.sidebar-link',
        headerAnchorSelector: '.header-anchor',
      },
    ],
    [
      '@vuepress/plugin-shiki',
      config.default.isProduction?
      {
        theme: 'github-dark', // 主题在线演示网址https://vscodethemes.com/
      }
      : false
    ],
    [
      '@vuepress/plugin-palette',
      {
        preset: 'sass'
      },
    ],
    [
      require(`./packages/md-enhance/node`),
      {
        enableAll: true,
        presentation: {
          plugins: [
            "highlight",
            "math",
            "search",
            "notes",
            "zoom",
            "anything",
            "audio",
            "chalkboard",
          ],
        },
      },
    ],
    ['vuepress-plugin-nprogress'],
  ],
});
